﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.IO;
using System.Drawing;
using SuperFind.Lib.Core;


namespace SuperFind
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private FileCache fileCache = null;
        public MainWindow()
        {
            InitializeComponent();
            DiskBot bot = new DiskBot();
            bot.Start();
        }
        private void menu_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mnu = e.OriginalSource as MenuItem;
            if(mnu!=null)
            {
                string commandParameter = ((System.Windows.Controls.MenuItem)(e.OriginalSource)).CommandParameter as string;
                Option optionWindow = null;
                switch (commandParameter)
                {
                    case "refresh_cache":
                        // Todo: call build cache function
                        FileCache fileCashe = new FileCache();
                        fileCashe.Refresh();
                        break;
                    case "select_locations":
                        ShowOption(1);
                        break;
                    case "preferences":
                        ShowOption();
                        break;
                    case "exit":
                        App.CloseApplication(true);
                        break;
                    default:
                        #if DEBUG
                            throw new NotImplementedException("Menu Item Not Implemented");
                        #endif
                        break;
                }
            }
                
        }

        private void ShowOption(int selectedTab=-1)
        {
            Option optionWindow;
            if(selectedTab>=0)
                optionWindow = new Option(selectedTab);
            else
                optionWindow = new Option();
            optionWindow.Owner = this;
            bool? result = optionWindow.ShowDialog();
            if (result??false)
            {
                optionWindow.SaveSettings();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (App.ShutdownApplication)
            {
                App.SystemTrayIcon.Visible = false;
            }
            else
            {
                e.Cancel = true;
                App.Current.MainWindow.Hide();
                App.ShowNotification(System.IO.Path.GetFileName(Assembly.GetEntryAssembly().GetName().Name + " is still running"));
            }
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (fileCache == null)
                fileCache = new FileCache();
            fileCache.FileFilter = txtSearch.Text;
            lstSearchResult.ItemsSource = fileCache.SearchResult;
        }
    }
}
